<?php

class Api_UserController extends Zend_Rest_Controller
{
	public function init()
	{
		$this->_helper->viewRenderer->setNoRender(true);
	}

	public function indexAction()
	{
		try{

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function getAction()
	{
		try{
			$user_id = $this->_getParam('id');
			if($user_id != ""){
				$user_mapper = new Api_Model_UserMapper();
				$user_e = $user_mapper->getUserById($user_id);

				$this->getresponse()->appendBody(json_encode($user_e));
				$this->getResponse()->setHttpResponseCode(200);
			} else{
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}


	}

	public function getcheckinAction() {
		try{
			$user_id = $this->_getParam('param1');
			if($user_id != ""){
				$user_mapper = new Api_Model_UserMapper;
				$loc = $user_mapper->getCheckInLocation($user_id);

				$this->getresponse()->appendBody(json_encode($loc));
				$this->getResponse()->setHttpResponseCode(200);
			} else{
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function otheruserAction()
	{
		try{
			$user_id = $this->_getParam('param1');
			$other_user_id = $this->_getParam('param2');
			if($user_id != "" && $other_user_id != ""){
				$user_mapper = new Api_Model_UserMapper();
				$user_e = $user_mapper->getOtherUserById($user_id, $other_user_id);

				$this->getresponse()->appendBody(json_encode($user_e));
				$this->getResponse()->setHttpResponseCode(200);
			} else{
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}


	}



	public function checkusernameAction()
	{
		try{
			$user_mapper = new Api_Model_UserMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);
			var_dump($data);
			if ($data['username'] != ""){
				$user_mapper->checkUsername($data);
			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}


	}

	public function postAction()
	{
		try{
			$user_mapper = new Api_Model_UserMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			if ($data['username'] != "" || $data['password'] != ""){

				$token = $user_mapper->createUser($data);
				$this->getresponse()->appendBody(json_encode($token));
				$this->getResponse()->setHttpResponseCode(201);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function createuserAction()
	{
		try{
			 
			$user_mapper = new Api_Model_UserMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			if ($data['username'] != "" || $data['password'] != ""){

				$token = $user_mapper->createUser($data);
				$this->getresponse()->appendBody(json_encode($token));
				$this->getResponse()->setHttpResponseCode(201);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function searchuserAction()
	{
		try{
			$user_mapper = new Api_Model_UserMapper();
			$user_id = $this->_getParam('param1');
			$username = $this->_getParam('key');

			if ($username != "" && $user_id != ''){

				$user = $user_mapper->searchUser($user_id, $username);
				$this->getresponse()->appendBody(json_encode($user));
				$this->getResponse()->setHttpResponseCode(201);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function loginAction()
	{
		try{
			$user_mapper = new Api_Model_UserMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);
			if ($data['username'] != "" && $data['password'] != ""){

				$token = $user_mapper->loginUser($data);
				$this->getresponse()->appendBody(json_encode($token));
				$this->getResponse()->setHttpResponseCode(200);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function logoutAction(){

		try{
			$user_mapper = new Api_Model_UserMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			if ($data['token'] != "" && $data['password'] != ""){
				$user_mapper->logoutUser();

				$this->getResponse()->setHttpResponseCode(200);
			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}
			 
		} catch (Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function putAction()
	{
		try{
			$user_id = $this->_getParam('id');
			$user_mapper = new Api_Model_UserMapper();
			if($user_id != ""){
				$action = $this->getRequest()->getHeader('action');
				if($action == 'updateinfo'){
					$rawBody = $this->getRequest()->getRawBody();
					$data = Zend_Json::decode($rawBody);
					$edit = $user_mapper->updateUser($data, $user_id);

					$this->getresponse()->appendBody(json_encode($edit));
					$this->getResponse()->setHttpResponseCode(200);
				} elseif($action == 'updateavatar'){
					$rawBody = $this->getRequest()->getRawBody();
					$data = Zend_Json::decode($rawBody);
					$edit = $user_mapper->updateavatarUser($data, $user_id);

					$this->getresponse()->appendBody(json_encode($edit));
					$this->getResponse()->setHttpResponseCode(200);
				} elseif($action == 'updatecover'){
					$rawBody = $this->getRequest()->getRawBody();
					$data = Zend_Json::decode($rawBody);
					$edit = $user_mapper->updateCoverUser($data, $user_id);

					$this->getresponse()->appendBody(json_encode($edit));
					$this->getResponse()->setHttpResponseCode(200);
				} else {
					$rawBody = $this->getRequest()->getRawBody();
					$data = Zend_Json::decode($rawBody);

					$token = $user_mapper->changepassUser($data['confirmPassword'], $data['password'], $user_id);
					$this->getresponse()->appendBody(json_encode($token));
					$this->getResponse()->setHttpResponseCode(200);
				}
			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}
			 
		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}


	public function deleteAction()
	{
		$this->getresponse()->appendBody('Delete Resources');
		$this->getResponse()->setHttpResponseCode(200);
	}

	public function changepassAction() {
		try{
			$user_id = $this->_getParam('id');
			if($user_id != ""){
				$user_mapper = new Api_Model_UserMapper();
				$rawBody = $this->getRequest()->getRawBody();
				$data = Zend_Json::decode($rawBody);
				$token = $user_mapper->changepassUser($data['password'], $user_id);
				$this->getresponse()->appendBody(json_encode($token));
				$this->getResponse()->setHttpResponseCode(200);
			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}
		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function headAction() {

	}

}
